package com.yzt.zhuju.utils;

import android.app.Activity;
import android.graphics.Color;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.bumptech.glide.Glide;
import com.yzt.zhuju.network.entity.chatroom.SvgaParam;
import com.opensource.svgaplayer.SVGADynamicEntity;
import com.opensource.svgaplayer.SVGAImageView;

import java.util.ArrayList;
import java.util.List;

/**
 * author : bob
 * date   : 2022/12/2
 * desc   : svga 工具类
 */
public class SvgaUtils {

    /**
     * Bofang动画
     */
    public static void loadSvga(SVGAImageView mSVGAImageView, String url) {
        loadSvga(mSVGAImageView, url, null);
    }

    public static void loadSvga(SVGAImageView mSVGAImageView, String url, OnCompleteListener listener) {

        if (mSVGAImageView == null || mSVGAImageView.getContext() == null || ((Activity) mSVGAImageView.getContext()).isDestroyed()) {
            return;
        }
        Glide.with(mSVGAImageView.getContext())
                .load(url)
                .into(mSVGAImageView);
    }

    public interface OnCompleteListener {
        void onComplete();
    }

    /**
     * 进行简单的文本替换
     */
    public static SVGADynamicEntity requestDynamicItem(String svgaConfig) {
        return requestDynamicItem(svgaConfig, Layout.Alignment.ALIGN_CENTER);
    }

    /**
     * 进行简单的文本替换
     */
    public static SVGADynamicEntity requestDynamicItem(String svgaConfig, Layout.Alignment textAlign) {
        if (!TextUtils.isEmpty(svgaConfig) && svgaConfig.length() > 0) {
            try {
                List<SvgaParam> list = JSON.parseObject(svgaConfig, new TypeReference<ArrayList<SvgaParam>>() {
                });
                SVGADynamicEntity dynamicEntity = new SVGADynamicEntity();
                for (SvgaParam param : list) {
                    switch (param.getType()) {
                        case 0:
                            //文字
                            TextPaint textPaint = new TextPaint();
                            textPaint.setColor(Color.parseColor(param.getTextColor()));
                            textPaint.setTextSize(param.getTextSize());
                            StaticLayout staticLayout = new StaticLayout(
                                    param.getValue(),
                                    0,
                                    param.getValue().length(),
                                    textPaint,
                                    0,
                                    textAlign,
                                    1.0f,
                                    0.0f,
                                    false
                            );
                            dynamicEntity.setDynamicText(staticLayout, param.getKey());
                            break;
                        case 1:
                            //设置图片
                            dynamicEntity.setDynamicImage(param.getValue(), param.getKey());
                            break;
                        default:
                            break;
                    }
                }
                return dynamicEntity;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
